<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/20 0020
 * Time: 22:56
 */

namespace app\admin\controller;
use app\admin\model\Admin as AdminModel;
use app\admin\model\Lab as LabModel;
use app\admin\model\Teacher;
use app\index\model\LabApp;
use think\Controller;
use think\Session;
use think\Validate;
use think\Db;

Class AdminLab extends Controller {
    //中间件登录限制
    protected $middleware = [
        'AdminCheck'
    ];
	//课室列表
	public function lab_list(){
		$name=input('get.name');
		$lab=new LabModel();
		if (isset($name)) {
			$lab=$lab->with('teacher')
                ->where('name','like','%'.$name.'%')
                ->paginate(10,false,['query'=>['name'=>$name]]);
		}else{
			$lab=$lab->with('teacher')->paginate(10);
		}
		$this->assign('labs',$lab);
		return $this->fetch();
	}
	//课室添加
	public function lab_add(){
		if ($this->request->isPost()) {
			$data=input('post.');
			$validate=new Validate();
			$rule=[
				'name|课室名称'=>'require|unique:lab',
				'address|课室地址'=>'require|max:20',
				'people|承载人数' =>'require|number',
                'teacher_id|管理教师'      =>'require'
			];
			$validates=$validate->check($data,$rule);
			if ($validates==true) {
				$lab=new LabModel();
				$lab=$lab->save([
					'name' => $data['name'],
					'address'=>$data['address'],
					'people' =>$data['people'],
                    'teacher_id'=>$data['teacher_id']
				]);
				if ($lab) {
					$msg=$this->msg(200,'添加课室成功');
				}else{
					$msg=$this->msg(400,'添加课室失败');
				}	
			}else{
				$msg=$this->msg(400,$validate->getError());
			}
			return $msg; 
		}
		$teacher=Teacher::all();
		$this->assign('teachers',$teacher);
		return $this->fetch();
	}
	//课室删除
	public function lab_dele(){
		$id=input('post.');
		$lab=LabModel::destroy($id);
		if ($lab) {
			$msg=$this->msg(200,'课室删除成功');
		}else{
			$msg=$this->msg(400,'课室删除失败');
		}
		return $msg;
	}
	//课室更新
	public function lab_update($id){
		$lab=new LabModel();
		$lab=$lab->where('id',$id)->find();
		$this->assign('lab',$lab);
		if ($this->request->isPost()) {
			$data=input('post.');
			$rule=[
				'name|课室名称'=>'require',
				'address|课室地址'=>'require',
				'people|承载人数' =>'require|number',
                'teacher_id|管理教师'=>'require'
			];
			$validate=new Validate();
			$va=$validate->check($data,$rule);
			if ($va==true) {
				$lab=$lab->save([
					'name'=>$data['name'],
					'address' =>$data['address'],
					'people'=>$data['people'],
                    'teacher_id'=>$data['teacher_id']
				],['id'=>$id]);
				if ($lab) {
					$msg=$this->msg(200,'更新成功');
				}else{
					$msg=$this->msg(400,'更新失败');
				}
			}else{
				$msg=$this->msg(400,$validate->getError());
			}
			return $msg;
		}
        $teacher=Teacher::all();
        $this->assign('teachers',$teacher);
		return $this->fetch();
	}
	//教师所属科室
    public function lab_teacher_list(){
	    $teacher_id=\session('teacher')['id'];
	    $lab=new LabModel();
	    $labs=$lab->where('teacher_id',$teacher_id)->paginate(10);
	    $this->assign('labs',$labs);
	    return $this->fetch();
    }
	public function msg($statu,$msg){
		return [
			'statu' =>$statu,
			'msg'   =>$msg
		];

	}

}